package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.PatientService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.Patient;
import com.his.dao.domain.entity.User;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.PatientMapper;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    private PatientMapper patientMapper;

    @Override
    public RestResult all(ListDto listDto) {
        Patient patient = MapUtil.conversionMapToObj((LinkedHashMap) listDto.getObj(), Patient.class);;
        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>()
                .select(Patient.class, info -> !info.getColumn().equals("password"))
                .like(patient.getName() != null, "name", patient.getName())
                .like(patient.getUsername() != null, "username", patient.getUsername())
                .like(patient.getId() != null, "ID", patient.getId());
        IPage hospitalPage = patientMapper.selectPage(iPage, queryWrapper);

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(hospitalPage.getRecords())
                .total(hospitalPage.getTotal())
                .build());
    }

    @Override
    public RestResult allId(Patient patient) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>()
                .select("patient_id", "name")
                .like(patient.getName() != null, "name", patient.getName());
        return new RestResultBuilder().success(patientMapper.selectList(queryWrapper));
    }

    @Override
    public RestResult idToName(Patient patient) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>()
                .eq(patient.getId() != null, "id", patient.getId());
        return new RestResultBuilder().success(patientMapper.selectOne(queryWrapper));
    }

    @Override
    public Integer getPatient(String patientCard, String patientName) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>()
                .eq("id", patientCard);
        Patient patient = patientMapper.selectOne(queryWrapper);
        if (patient == null) {
            patient = Patient
                    .builder()
                    .name(patientName)
                    .username(patientCard)
                    .id(patientCard)
                    .createTime(LocalDateTime.now())
                    .build();
            save(patient);
            patient = patientMapper.selectOne(queryWrapper);
            return patient.getPatientId();
        }

        return patient.getPatientId();
    }
}
